package com.example.demo.service;

import com.example.demo.bean.Book;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Future;

public class BookService {
    @Autowired
    private RestTemplate restTemplate;

    //在test10方法上添加@HystrixCollapser注解实现请求合并，
    // 用batchMethod属性指明请求合并后的处理方法，
    // collapserProperties属性指定其他属性。
    @HystrixCollapser(batchMethod = "test11", collapserProperties = {@HystrixProperty(name = "timerDelayInMilliseconds", value = "100")})
    public Future<Book> test10(Long id) {
        return null;
    }

    @HystrixCommand
    public List<Book> test11(List<Long> ids) {
        System.out.println("test9---------" + ids + "Thread.currentThread().getName():" + Thread.currentThread().getName());
        Book[] books = restTemplate.getForObject("http://HELLO-SERVICE/getbook6?ids={1}", Book[].class, StringUtils.join(ids, ","));
        return Arrays.asList(books);
    }
}
